import Axios,{AxiosResponse,AxiosRequestConfig,AxiosRequestHeaders} from 'axios';
import {IResData} from "@/models/ResponseData"
export const request = Axios.create({
    baseURL:'/api',
    // timeout:3
});
interface Test{
    // token:string; 严格
    [k:string]:any; // 宽泛
}

// 拦截器
request.interceptors.request.use(config=>{
    const token = window.sessionStorage.getItem('token');
    if (token) {
        // const conf:AxiosRequestConfig<any> = config;
        //     // 或者undefined   断言成我们的类型
        // (conf.headers as any as Test).token=""
        (config.headers as any).token = token;
    }
    return config
})
//IResData
request.interceptors.response.use((response:AxiosResponse<IResData>)=>{
    // 响应 回来有token 
    console.log('res:拦截器',response.data);
    
    const token = response.data.data.token;  
    if(token) {
        window.sessionStorage.setItem('token',token);
    }
    return response;
})